<template>
  <!-- 
    el-upload  
         action:上传的接口地址(必传)
         name:上传文件的参数名
         show-file-list:是否显示上传文件列表
         before-upload:上传前的回调,限制上传文件大小与类型
         on-success:上传成功的回调
         默认插槽：显示的内容
         http-request：自定义上传形为

  完整上传
      1: 上传
          放到全局组件中
      2：回显上传图片
      3：删除上传图片
      4：预览
      5：进度条
   -->
  <div>
    <el-upload
      class="avatar-uploader"
      action="http://119.91.150.211:8080/heimamm/public/uploads"
      name="image"
      :show-file-list="false"
      :before-upload="beforeUpload"
      :on-success="onSuccess"
    >
      <img v-if="imageUrl" :src="imageUrl" class="avatar" />
      <i v-else class="el-icon-plus avatar-uploader-icon"></i>
    </el-upload>
  </div>
</template>
<script>
export default {
  data () {
    return {
      imageUrl: ''
    }
  },
  methods: {
    beforeUpload (file) {
      // 限制文件大小不能超过100k
      const limitSize = file.size / 1024 < 100
      if (!limitSize) {
        this.$message.error('请上传小于100kb的文件')
      }
      // 限制只能上传png
      const limitType = file.type === 'image/png'
      if (!limitType) {
        this.$message.error('请上传PNG文件')
      }
      return limitSize && limitType
    },
    onSuccess (res) {
      this.imageUrl =
        'http://119.91.150.211:8080/heimamm/public/' + res.data.file_path
      console.log(res)
    }
  }
}
</script>
<style>
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}
.avatar {
  width: 178px;
  height: 178px;
  display: block;
}
</style>
